package com.hingsmy.pma.controllers;

import com.hingsmy.pma.dao.ProjectRepository;
import com.hingsmy.pma.dao.StudentRepository;
import com.hingsmy.pma.entities.Project;
import com.hingsmy.pma.entities.Student;
import com.hingsmy.pma.services.ProjectService;
import com.hingsmy.pma.services.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Iterator;
import java.util.List;
import java.util.Optional;

@Controller
//导航到http://localhost:8080/projects
@RequestMapping("projects")
public class ProjectController {
    @Autowired
    ProjectService projectService;
    @Autowired
    StudentService studentService;
    @GetMapping
    public String displayProjects(Model model) {
        List<Project> projects=projectService.getAll();
        model.addAttribute("projects", projects);
        return "projects/list-projects";
    }
    //导航到http://localhost:8080/projects /new
    @GetMapping("/new")
    public String displayProjectForm(Model model) {
        Project aProject=new Project();
        List<Student> students=studentService.getAll();
        model.addAttribute("project", aProject);
        model.addAttribute("allStudents",students);
        return "projects/new-project";
    }
    //导航到http://localhost:8080/projects/save
        @PostMapping("/save")
                public String createProject(Model model ,@RequestParam List<Long> students , Project project){
            //处理保存到数据库的行为
            projectService.save(project);


            return "redirect:/projects";
        }
    }



